Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 1x 1x 4x 1x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 6x 6x 6x 6x 6x 5x 6x 2x 2x 3x 3x 3x 3x 3x 6x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x | import { NextRequest, NextResponse } from 'next/server';
import { getAdminClient } from '@/lib/supabase/admin';
import { createHash } from 'crypto';
interface SubmitParams {
params: Promise<{ token: string }>;
}
// GET - Validate token and get request details
export async function GET(request: NextRequest, { params }: SubmitParams) {
try {
const { token } = await params;
const adminClient = getAdminClient();
// Find request by token
const { data: requestData, error } = await adminClient
.from('testimonial_requests')
.select(
`
id,
recipient_name,
relationship_context,
status,
expires_at,
profile_id,
profiles:profile_id (
display_name,
avatar_url,
role
)
`
)
.eq('token', token)
.single();
if (error || !requestData) {
return NextResponse.json({ error: 'Invalid or expired link' }, { status: 404 });
}
// Check if expired
if (new Date(requestData.expires_at) < new Date()) {
return NextResponse.json({ error: 'This link has expired' }, { status: 410 });
}
// Check if already submitted
if (requestData.status === 'submitted' || requestData.status === 'approved') {
return NextResponse.json({ error: 'Testimonial already submitted' }, { status: 400 });
}
// Update opened_at if not set
if (!requestData.status || requestData.status === 'pending') {
await adminClient
.from('testimonial_requests')
.update({ opened_at: new Date().toISOString() })
.eq('id', requestData.id);
}
return NextResponse.json({
request: {
id: requestData.id,
recipientName: requestData.recipient_name,
relationshipContext: requestData.relationship_context,
profile: requestData.profiles,
},
});
} catch (error) {
console.error('Testimonial submit GET error:', error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}
// POST - Submit testimonial
export async function POST(request: NextRequest, { params }: SubmitParams) {
try {
const { token } = await params;
const adminClient = getAdminClient();
const body = await request.json();
const { author_name, author_title, author_email, content, rating, relationship_context } = body;
// Validate required fields
if (!author_name || !content) {
return NextResponse.json({ error: 'Author name and content are required' }, { status: 400 });
}
// Find request by token
const { data: requestData, error: requestError } = await adminClient
.from('testimonial_requests')
.select('*')
.eq('token', token)
.single();
if (requestError || !requestData) {
return NextResponse.json({ error: 'Invalid or expired link' }, { status: 404 });
}
// Check if expired
if (new Date(requestData.expires_at) < new Date()) {
return NextResponse.json({ error: 'This link has expired' }, { status: 410 });
}
// Check if already submitted
if (requestData.status !== 'pending') {
return NextResponse.json({ error: 'Testimonial already submitted' }, { status: 400 });
}
// Get IP hash for abuse detection
const ip = request.headers.get('x-forwarded-for') || request.headers.get('x-real-ip') || '';
const ipHash = createHash('sha256').update(ip).digest('hex').substring(0, 16);
// Check IP rate limit (3 submissions per day per IP)
const { data: canSubmit } = await adminClient.rpc('check_rate_limit', {
p_key_type: 'ip_submissions',
p_key_value: ipHash,
p_limit: 3,
});
if (!canSubmit) {
return NextResponse.json(
{ error: 'Too many submissions. Please try again later.' },
{ status: 429 }
);
}
// Hash email for duplicate detection (if provided)
const emailHash = author_email
? createHash('sha256').update(author_email.toLowerCase()).digest('hex')
: null;
// Create testimonial
const { data: testimonial, error: testimonialError } = await adminClient
.from('testimonials')
.insert({
profile_id: requestData.profile_id,
request_id: requestData.id,
author_name,
author_title: author_title || null,
content,
rating: rating || null,
relationship_context: relationship_context || requestData.relationship_context,
verification_type: 'unverified',
moderation_status: 'pending_review',
author_email_hash: emailHash,
ip_hash: ipHash,
is_verified: false,
display_order: 0,
})
.select()
.single();
if (testimonialError) {
console.error('Error creating testimonial:', testimonialError);
return NextResponse.json({ error: 'Failed to submit testimonial' }, { status: 500 });
}
// Update request status
await adminClient
.from('testimonial_requests')
.update({
status: 'submitted',
submitted_at: new Date().toISOString(),
testimonial_id: testimonial.id,
})
.eq('id', requestData.id);
// Increment rate limit
await adminClient.rpc('increment_rate_limit', {
p_key_type: 'ip_submissions',
p_key_value: ipHash,
});
return NextResponse.json({
success: true,
message: 'Testimonial submitted successfully',
requiresApproval: true,
});
} catch (error) {
console.error('Testimonial submit POST error:', error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}
|